/**
 * 
 */
package com.gua.jbs.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gua.jbs.service.IUploadService;
import com.gua.jbs.serviceImpl.UploadService;
import com.gua.jbs.utility.ApplicationConstant;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.StringUtil;


/**
 * @author Yogesh 
 * upload excel,read and write excel
 *
 */
public class UploadController extends BaseController{
	
	private static final Logger logger = Logger
			.getLogger(LoginController.class);
	
	private String message;	
	private IUploadService uploadService=null;
	public UploadController(){
		uploadService=new UploadService();
	}
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 
		String action=request.getParameter(ApplicationConstant.ACTION);
		String fileName=request.getParameter(ApplicationConstant.UP_PROD_TYPE);
		if(StringUtil.isNullOrEmpty(action) && action.equalsIgnoreCase(ApplicationConstant.UPLOAD_PROD_TYPE)){
			fileName="Book1.xlsx";
			try {
				boolean isUplaodProdType=uploadService.uploadProdType(fileName);
				if(isUplaodProdType){
					message=ApplicationConstant.UPLOAD_EXCEL_SUCCUSSFUL;
					logger.info(message);
					response.sendRedirect("/pages/jsp/upload.jsp");
					
				}
				else{
					message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
					request.setAttribute("message",message);
					logger.info(message);
					rd = request.getRequestDispatcher("/pages/jsp/upload.jsp");
					rd.forward(request, response);
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in Application UploadController isUplaodProdType--->"+e);
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in UploadController isUplaodProdType--->"+e);
				e.printStackTrace();
			}
		}else if(StringUtil.isNullOrEmpty(action) && action.equalsIgnoreCase(ApplicationConstant.UPLOAD_PROD_SUBTYPE)){
			
			try {
				boolean isUplaodProdSubType=uploadService.uploadProdSubType(fileName);
				if(isUplaodProdSubType){
					message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;	
					logger.info(message);
					response.sendRedirect("/pages/jsp/upload.jsp");
					
				}
				else{
					message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
					logger.info(message);
					
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in Application UploadController isUplaodProdSubType()--->"+e);
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in UploadController AddUser()--->"+e);
				e.printStackTrace();
			}
		}else if(StringUtil.isNullOrEmpty(action) && action.equalsIgnoreCase(ApplicationConstant.UPLOAD_PROD_COLOR)){
			
			try {
				boolean isUplaodProdColor=uploadService.uploadProdColor(fileName);
				if(isUplaodProdColor){
					message=ApplicationConstant.UPLOAD_EXCEL_SUCCUSSFUL;	
					logger.info(message);
					response.sendRedirect("/pages/jsp/upload.jsp");
					
				}
				else{
					message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
					
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info(message);
				logger.info("Exception in Application UploadController isUplaodProdColor--->"+e);
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in UploadController isUplaodProdColor--->"+e);
				e.printStackTrace();
			}
		}else if(StringUtil.isNullOrEmpty(action) && action.equalsIgnoreCase(ApplicationConstant.UPLOAD_PROD_POLISH)){
			
			try {
				boolean isUplaodProdPolish=uploadService.uploadProdPolish(fileName);
				if(isUplaodProdPolish){
					message=ApplicationConstant.UPLOAD_EXCEL_SUCCUSSFUL;
					logger.info(message);
					response.sendRedirect("/pages/jsp/upload.jsp");
					
				}
				else{
					message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
					logger.info(message);
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in Application UploadController isUplaodProdPolish--->"+e);
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.UPLOAD_EXCEL_UNSUCCUSSFUL;
				logger.info("Exception in UploadController isUplaodProdPolish--->"+e);
				e.printStackTrace();
			}
		}	
	}
}
